package androidvn.xenforo.fourums.adapter;

import java.util.ArrayList;

import androidvn.xenforo.fourums.R;
import androidvn.xenforo.fourums.model.Node;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpListAdapterRight extends BaseExpandableListAdapter {

	Context context;
	ArrayList<Node> parentList;
	ArrayList<ArrayList<Node>> childList;

	public ExpListAdapterRight(Context context, ArrayList<Node> listParent,
			ArrayList<ArrayList<Node>> listChild) {
		super();
		this.context = context;
		this.parentList = listParent;
		this.childList = listChild;
	}

	@Override
	public Node getChild(int groupPosition, int childPosition) {

		return childList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {

		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		String child = getChild(groupPosition, childPosition).getTitle();
		ViewHolder holder = null;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.right_menu_list_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.tvTitle);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.title.setText(child);
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {

		return childList.get(groupPosition).size();
	}

	@Override
	public Node getGroup(int groupPosition) {
		return parentList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return parentList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {

		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup viewGroup) {
		Node parent = getGroup(groupPosition);
		ViewHolder holder = null;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.right_menu_list_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.tvTitle);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.title.setText(parent.getTitle());
		return convertView;
	}

	@Override
	public boolean hasStableIds() {

		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {

		return true;
	}

	static class ViewHolder {
		TextView title;
	}
}
